﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace WindowsFormsApplication1
{
    public delegate void refreshDef();

    public class PricesData
    {
        public Dictionary<int, OneRowData> MyDataDico = new Dictionary<int, OneRowData>();
        public List<int> MyDataDico_ChangedRows = new List<int>();

        private BindingList<OneRowData> PricesBinding = new BindingList<OneRowData>();    
        public System.Windows.Forms.DataGridView MyDataGridView;
        private refreshDef MyrefreshDel;
        
        public void setTheDataGridView(System.Windows.Forms.DataGridView theDataGridView)
        {
            MyDataGridView = theDataGridView;
            MyDataGridView.DataSource = PricesBinding;
            MyrefreshDel = MyDataGridView.Refresh;
        }

        public void add_Row(int ID, Double Value)
        {
            lock (MyDataDico)
            {
                OneRowData currRow;
                if (MyDataDico.TryGetValue(ID, out currRow))
                {
                    currRow.Value = Value;
                }
                else
                {
                    currRow = new OneRowData();
                    currRow.ID = ID;
                    currRow.Value = Value;
                    currRow.rowIndex = PricesBinding.Count;
                    PricesBinding.Add(currRow);
                    MyDataDico.Add(ID, currRow);
                }

                MyDataGridView.Refresh();
            }
        }

        public void UpdateTab(int ID, Double Value)
        {
            //lock (MyDataDico)
            //{
            lock (MyDataDico_ChangedRows)
            {
                OneRowData currRow;// = new OneRowData();
                if (MyDataDico.TryGetValue(ID, out currRow))
                {
                    currRow.Value = Value;
                    
                   
                    MyDataDico_ChangedRows.Add(currRow.rowIndex);
                 }

               }
            //}
            
        }

        public class OneRowData
        {
            public int ID{get;set;}
            public double Value{get;set;}
            public int rowIndex;
        }
    }
}
